#!/usr/bin/env python
# encoding: utf-8

from feature_set.base_feature import BaseFeature, RequstData


class UiUnBaseV1(BaseFeature):
    def __init__(self):
        super().__init__()
        self.function_map = {
            'user_info_1':self.gen_user_info_feature
        }

    def load_conf(self):
        """
        load config file
        Returns:

        """
        pass

    def load_request(self, request_data: RequstData):
        """
        get request data and format feature input data
        Args:
            request_data: input detail data

        Returns:
            format user_info
        """

        self.data = request_data.data_sources['user_info_1']
        self.logger.info(f'user_info_1:{self.data}')

    def gen_user_info_feature(self):
        if type(self.data) != dict:
            self.logger.error('user_info_1节点数据不是dict类型！')
            return {}

        userInfo = self.data['userInfo']
        if type(userInfo) != dict:
            self.logger.error('userInfo节点数据不是dict类型！')
            return {}

        userRecord = userInfo['userRecord']
        if type(userRecord) != dict:
            self.logger.error('userRecord节点数据不是dict类型！')
            return {}

        user_info_dict = {}
        numberical_feature = ['gender', 'age', 'workType', 'educationLevel', 'marriageStatus', 'monthlyIncome']
        categorical_feature = ['homeProvince','homeCity']
        for fea_key in numberical_feature:
            if fea_key in userRecord:
                user_info_dict[fea_key] = userRecord[fea_key]
            else:
                user_info_dict[fea_key] = -999

        for fea_key in categorical_feature:
            if fea_key in userRecord:
                user_info_dict[fea_key] = str(userRecord[fea_key]).lower()
            else:
                user_info_dict[fea_key] = ''
        return user_info_dict

